home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / tests / iconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  5.5 KB  |  189 lines

  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2.    This file is part of the GNU ICONV Library.
  3.  
  4.    The GNU ICONV Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.  
  9.    The GNU ICONV Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU ICONV Library; see the file COPYING.LIB.  If not,
  16.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.    Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <iconv.h>
  24. #include <errno.h>
  25.  
  26. static void usage ()
  27. {
  28.   fprintf(stderr,"Usage: iconv -f fromcode -t tocode [file ...]\n");
  29.   exit(1);
  30. }
  31.  
  32. static int convert (iconv_t cd, FILE* infile, const char* infilename)
  33. {
  34.   char inbuf[4096+4096];
  35.   size_t inbufrest = 0;
  36.   char outbuf[4096];
  37.   iconv(cd,NULL,NULL,NULL,NULL);
  38.   for (;;) {
  39.     size_t inbufsize = fread(inbuf+4096,1,4096,infile);
  40.     if (inbufsize == 0) {
  41.       if (inbufrest == 0)
  42.         break;
  43.       else {
  44.         fprintf(stderr,"iconv: %s: incomplete character or shift sequence\n",infilename);
  45.         return 1;
  46.       }
  47.     } else {
  48.       const char* inptr = inbuf+4096-inbufrest;
  49.       size_t insize = inbufrest+inbufsize;
  50.       inbufrest = 0;
  51.       while (insize > 0) {
  52.         char* outptr = outbuf;
  53.         size_t outsize = sizeof(outbuf);
  54.         size_t res = iconv(cd,&inptr,&insize,&outptr,&outsize);
  55.         if (outptr != outbuf) {
  56.           int saved_errno = errno;
  57.           if (fwrite(outbuf,1,outptr-outbuf,stdout) < outptr-outbuf)
  58.             return 1;
  59.           errno = saved_errno;
  60.         }
  61.         if (res == (size_t)(-1)) {
  62.           if (errno == EILSEQ) {
  63.             fprintf(stderr,"iconv: %s: cannot convert\n",infilename);
  64.             return 1;
  65.           } else if (errno == EINVAL) {
  66.             if (inbufsize == 0 || insize > 4096) {
  67.               fprintf(stderr,"iconv: %s: incomplete character or shift sequence\n",infilename);
  68.               return 1;
  69.             } else {
  70.               inbufrest = insize;
  71.               if (insize > 0) {
  72.                 /* Like memcpy(inbuf+4096-insize,inptr,insize), except that
  73.                    we cannot use memcpy here, because source and destination
  74.                    regions may overlap. */
  75.                 char* restptr = inbuf+4096-insize;
  76.                 do { *restptr++ = *inptr++; } while (--insize > 0);
  77.               }
  78.               break;
  79.             }
  80.           } else if (errno != E2BIG) {
  81.             int saved_errno = errno;
  82.             fprintf(stderr,"iconv: %s: ",infilename);
  83.             errno = saved_errno;
  84.             perror("");
  85.             return 1;
  86.           }
  87.         }
  88.       }
  89.     }
  90.   }
  91.   {
  92.     char* outptr = outbuf;
  93.     size_t outsize = sizeof(outbuf);
  94.     size_t res = iconv(cd,NULL,NULL,&outptr,&outsize);
  95.     if (outptr != outbuf) {
  96.       int saved_errno = errno;
  97.       if (fwrite(outbuf,1,outptr-outbuf,stdout) < outptr-outbuf)
  98.         return 1;
  99.       errno = saved_errno;
  100.     }
  101.     if (res == (size_t)(-1)) {
  102.       if (errno == EILSEQ) {
  103.         fprintf(stderr,"iconv: %s: cannot convert\n",infilename);
  104.         return 1;
  105.       } else if (errno == EINVAL) {
  106.         fprintf(stderr,"iconv: %s: incomplete character or shift sequence\n",infilename);
  107.         return 1;
  108.       } else {
  109.         int saved_errno = errno;
  110.         fprintf(stderr,"iconv: %s: ",infilename);
  111.         errno = saved_errno;
  112.         perror("");
  113.         return 1;
  114.       }
  115.     }
  116.   }
  117.   if (ferror(infile)) {
  118.     fprintf(stderr,"iconv: %s: I/O error\n",infilename);
  119.     return 1;
  120.   }
  121.   return 0;
  122. }
  123.  
  124. int main (int argc, char* argv[])
  125. {
  126.   const char* fromcode = NULL;
  127.   const char* tocode = NULL;
  128.   iconv_t cd;
  129.   int i;
  130.   int status;
  131.  
  132.   for (i = 1; i < argc;) {
  133.     if (!strcmp(argv[i],"-f")) {
  134.       if (i == argc-1) usage();
  135.       if (fromcode != NULL) usage();
  136.       fromcode = argv[i+1];
  137.       i += 2;
  138.       continue;
  139.     }
  140.     if (!strcmp(argv[i],"-t")) {
  141.       if (i == argc-1) usage();
  142.       if (tocode != NULL) usage();
  143.       tocode = argv[i+1];
  144.       i += 2;
  145.       continue;
  146.     }
  147.     if (argv[i][0] == '-')
  148.       usage();
  149.     break;
  150.   }
  151.   if (fromcode == NULL || tocode == NULL)
  152.     usage();
  153.   cd = iconv_open(tocode,fromcode);
  154.   if (cd == (iconv_t)(-1)) {
  155.     if (iconv_open("UCS-4",fromcode) == (iconv_t)(-1))
  156.       fprintf(stderr,"iconv: conversion from %s unsupported\n",fromcode);
  157.     else if (iconv_open(tocode,"UCS-4") == (iconv_t)(-1))
  158.       fprintf(stderr,"iconv: conversion to %s unsupported\n",tocode);
  159.     else
  160.       fprintf(stderr,"iconv: conversion from %s to %s unsupported\n",fromcode,tocode);
  161.     exit(1);
  162.   }
  163.   if (i == argc)
  164.     status = convert(cd,stdin,"(stdin)");
  165.   else {
  166.     status = 0;
  167.     for (; i < argc; i++) {
  168.       const char* infilename = argv[i];
  169.       FILE* infile = fopen(infilename,"r");
  170.       if (infile == NULL) {
  171.         int saved_errno = errno;
  172.         fprintf(stderr,"iconv: %s: ",infilename);
  173.         errno = saved_errno;
  174.         perror("");
  175.         status = 1;
  176.       } else {
  177.         status |= convert(cd,infile,infilename);
  178.         fclose(infile);
  179.       }
  180.     }
  181.   }
  182.   iconv_close(cd);
  183.   if (ferror(stdout)) {
  184.     fprintf(stderr,"iconv: I/O error\n");
  185.     status = 1;
  186.   }
  187.   exit(status);
  188. }
  189.